package com.example.selectphoto_8;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private static final int		REQ_TAKE_CODE	= 100;
	private static final int		REQ_PICK_CODE	= 101;
	private static final int		REQ_ZOOM_CODE	= 102;
	private RelativeLayout			mRl_root;
	private View					mViewMask;
	private SelectPicPopupWindow	mPhotoPopupWindow;
	private CircleImageView			mCiv;
	private String					mTempPath		= Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
															+ "tempFile";
	private File					mTempFile		= new File(mTempPath);
	private CheckBox				mCb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mViewMask = findViewById(R.id.viewMask);
		mRl_root = (RelativeLayout) findViewById(R.id.rl_root);
		mCiv = (CircleImageView) findViewById(R.id.civ);
		mCb = (CheckBox) findViewById(R.id.cb);
		mViewMask.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPhotoPopupWindow.dismiss();
				AnimationUtils.hideAlpha(mViewMask);
			}
		});
		mCiv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mCb.isChecked()) {//用弹出框
					PhotoUtilChange.getPhotoDialog(MainActivity.this, REQ_TAKE_CODE, REQ_PICK_CODE, mTempFile);
				} else {//用底部下滑框
					mPhotoPopupWindow = PhotoUtilChange.getPicPopupWindow(MainActivity.this, MainActivity.this,
							mRl_root);
				}
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		if (mPhotoPopupWindow != null) {
			mPhotoPopupWindow.dismiss();
			AnimationUtils.hideAlpha(mViewMask);
		}
		switch (v.getId()) {
		case R.id.btn_pick_photo:
			PhotoUtilChange.pickPhoto(this, REQ_PICK_CODE, mTempFile);
			break;
		case R.id.btn_take_photo:
			PhotoUtilChange.takePhoto(this, REQ_TAKE_CODE, mTempFile);
			break;
		case R.id.btn_cancel:
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQ_TAKE_CODE:// 拍照完成之后会来到这个地方
			PhotoUtilChange.onPhotoFromCamera(this, REQ_ZOOM_CODE, mTempPath, 1, 1);
			break;
		case REQ_PICK_CODE:// 从相册选择好之后的结果
			PhotoUtilChange.onPhotoFromPick(this, REQ_ZOOM_CODE, mTempPath, data, 1, 1);
			break;
		case REQ_ZOOM_CODE:// 缩放完成之后会来到这个地方
			Bitmap zoomBitMap = PhotoUtilChange.getZoomBitMap(data, this);
			mCiv.setImageBitmap(zoomBitMap);
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
